package com.wjy.dao;

import com.wjy.exception.business.BusinessException;
import com.wjy.jdbc.SQLUtil;
import com.wjy.util.RandomCodeUtil;
import com.wjy.vo.Author;
import lombok.extern.log4j.Log4j2;

import java.util.List;

@Log4j2
public class AuthorDao extends SQLUtil {

    public Author login(Author author) throws Exception {

        String sql = "SELECT author.author_id, author.author_account, author.author_password, author.author_name, author.author_sex, "
                + "author.author_birthday, author.author_phone, author.author_email, author.author_photo, author.author_state "
                + "FROM author WHERE author.author_account = ? AND author.author_password = ?";

        Object[] objects = new Object[]{author.getAuthor_account(), author.getAuthor_password()};

        List<Author> authorList = Query(sql, objects, Author.class);

        if (authorList.size() == 0) {

            throw new BusinessException("账号或密码错误，登录失败");

        }

        Author a = authorList.get(0);

        log.info("Author:" + a.toString());

        return a;

    }

    public int verify(String authorAccount) throws Exception {

        String sql = "SELECT author.author_id, author.author_account, author.author_password, author.author_name, author.author_sex, "
                + "author.author_birthday, author.author_phone, author.author_email, author.author_photo, author.author_state "
                + "FROM author WHERE author.author_account = ?";

        Object[] objects = new Object[]{authorAccount};

        List<Author> authorList = Query(sql, objects, Author.class);

        if (authorList.size() != 0) {

            throw new BusinessException("账号已存在，请重新输入");

        }

        int num = authorList.size();

        return num;

    }

    public int register(Author author) throws Exception {

        String sql = "INSERT INTO AUTHOR(author.author_id, author.author_account, author.author_password, author.author_name, author.author_sex, "
                + "author.author_birthday, author.author_phone, author.author_email, author.author_photo, author.author_state) "
                + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        int num = Update(sql, RandomCodeUtil.getUUID(), author.getAuthor_account(), author.getAuthor_password(),
                author.getAuthor_name(), author.getAuthor_sex(), author.getAuthor_birthday(), author.getAuthor_phone(),
                author.getAuthor_email(), "logo.jpg", 1);

        if (num == 0) {

            throw new BusinessException("注册失败，请重新注册");

        }

        return num;

    }

}
